import java.util.Vector;

import soot.Unit;

/**
 * basic data structre of a Index Path
 * 
 * @author Steven Hsui
 * 
 */
public class IndexPath {
	/**
	 * all the nodes of one path
	 */
	private Vector<IndexNode> nodes;
	
	/**
	 * record the call points which shall be extended of the path
	 */
	private Vector<String> callPoints;

	public IndexPath() {
		this.nodes = new Vector<IndexNode>();
		this.callPoints = new Vector<String>();
	}

	public IndexPath(Vector<IndexNode> nodes, Vector<String> called) {
		this.nodes = nodes;
		this.callPoints = called;
	}

	public void addNode(IndexNode node) {
		this.nodes.add(node);
	}

	public IndexNode getNode(int index) {
		return this.nodes.get(index);
	}

	public boolean contains(int line) {
		return false;
	}

	public boolean contains(Unit unit) {
		for (int i = 0; i < nodes.size(); i++) {
			IndexNode node = nodes.get(i);
			if (node.getUnit().equals(unit)) {
				return true;
			}
		}
		return false;
	}

	public boolean contains(String nodeValue) {
		for (int i = 0; i < nodes.size(); i++) {
			IndexNode node = nodes.get(i);
			if (node.getValue().equals(nodeValue)) {
				return true;
			}
		}
		return false;
	}

	public int getIndex(String nodeValue) {
		for (int i = 0; i < nodes.size(); i++) {
			IndexNode node = nodes.get(i);
			if (node.getValue().equals(nodeValue)) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public IndexPath clone() {
		Vector<IndexNode> cloneNodes = new Vector<IndexNode>();
		Vector<String> called = new Vector<String>();
		for (int i = 0; i < nodes.size(); i++) {
			cloneNodes.add(nodes.get(i));
		}
		
		for (int i = 0; i < callPoints.size(); i++) {
			called.add(callPoints.get(i));
		}
		
		IndexPath path = new IndexPath(cloneNodes, called);
		return path;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < this.nodes.size(); i++) {
			sb.append(nodes.get(i).getValue() + " "
					+ nodes.get(i).getUnit().toString() + " "
					+ nodes.get(i).getShouldRecord() + " "
					+ nodes.get(i).isLeaf() + "\n");
		}
		return sb.toString();
	}

	public IndexNode getLast() {
		return this.nodes.get(this.nodes.size() - 1);
	}

	public int size() {
		return nodes.size();
	}

	public int CallPointsSize() {
		return callPoints.size();
	}

	public String GetCallPoint(int index) {
		return callPoints.get(index);
	}

	public boolean isCalled(String cm) {
		return callPoints.contains(cm);
	}

	public void called(String cm) {
		callPoints.add(cm);
	}

	public int IndexOf(String value) {
		for (int i = 0; i < nodes.size(); i++) {
			if (nodes.get(i).getValue().equals(value)) {
				return i;
			}
		}
		return -1;
	}
}
